﻿using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.Examine;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.Examine
{
    public class QcProjectClassifyServices : IQcProjectClassifyServices
    {
        public QcProjectClassifyServices(IBaseRepository db)
        {
            Db = db;
        }

        public IBaseRepository Db { get; }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> AddAsync(QcProjectClassify model)
        {
            //插入单条数据
            var result = await Db.InsertAsync(model);
            return result;
        }
        //批量删除
        public async Task<bool> DeleteAsync(int[] Ids)
        {
            //批量删除数据
            string sql = @"
                        with tab as(
	                        select * from QcProjectClassify where Id in @Ids
	                        union all 
	                        select child.* from QcProjectClassify child join tab parent on parent.Id = child.ParentClassify
                        )
                        delete QcProjectClassify where id in(select id from tab)";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
            /*
             string sql = "delete TeamType where Id in @Ids";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
             */
        }
        //单条删除
        public async Task<bool> DeleteAsync(int Id)
        {
            /*
             string sql = "delete TeamType where Id = @Id";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
             */
            string sql = @"
                        with tab as(
	                        select * from QcProjectClassify where Id =@Id
	                        union all 
	                        select child.* from QcProjectClassify child join tab parent on parent.Id = child.ParentClassify
                        )
                        delete QcProjectClassify where id in(select id from tab)";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }
        /// <summary>
        ///查询所有数据
        /// </summary>
        /// <returns></returns>
        public async Task<IEnumerable<QcProjectClassify>> GetAllAsync()
        {
            string sql = "select * from QcProjectClassify";
            var list = await Db.QueryAllAsync<QcProjectClassify>(sql);
            return list;
        }

        //获取所有数据
        public async Task<IEnumerable<QcProjectClassify>> GetAsync(string ProjectCode, string ProjectName)
        {
            string sql = "select * from QcProjectClassify where 1=1";
            if (!string.IsNullOrEmpty(ProjectCode))
            {
                sql += "and ProjectCode like @ProjectCode";
            }
            if (!string.IsNullOrEmpty(ProjectName))
            {
                sql += "and ProjectName like @ProjectName";
            }
            var list = await Db.QueryAsync<QcProjectClassify>(sql, new { ProjectCode = $"%{ProjectCode}%", ProjectName = $"%{ProjectName}%" });
            return list;
        }
        /*
         public async Task<IEnumerable<QcProjectClassify>> GetAllAsync()
        {
            string sql = "select * from TeamType";
            var list = await Db.QueryAllAsync<QcProjectClassify>(sql);
            return list;
        }
         */
        public async Task<bool> UpdateAsync(QcProjectClassify model)
        {
            var result = await Db.UpdateAsync(model);
            return result;
        }
    }
}
